package com.dgshanger.sanhxiaofeisc.mvp.presenter

import android.text.TextUtils
import com.dgshanger.sanhxiaofeisc.App.Companion.application
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.entity.CheckPwdEntity
import com.dgshanger.sanhxiaofeisc.entity.TransferAccountsEntity
import com.dgshanger.sanhxiaofeisc.entity.UserEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.TransferAccountsContract
import com.dgshanger.sanhxiaofeisc.mvp.model.TransferAccountsModel
import com.dgshanger.sanhxiaofeisc.ui.person.purse.TransferAccountsSuccessActivity
import com.xx.baseuilibrary.mvp.BaseRxMvpPresenter
import com.xx.baseutilslibrary.network.rx.RxHttpObserver

/**
 * TransferAccountsPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/5/3 15:51.
 */

class TransferAccountsPresenter : BaseRxMvpPresenter<TransferAccountsContract.Model, TransferAccountsContract.View>(), TransferAccountsContract.Presenter {


    override fun loadUser(user_mobile: String?) {
        getModel().loadUser(user_mobile, object : RxHttpObserver<UserEntity>() {
            override fun onCompleted(msg: String?, entity: UserEntity?) {
                getView()?.setUserData(entity)
            }

            override fun onError(error: String?) {
            }

            override fun onLoadingStart() {
            }

            override fun onLoadingFinish() {

            }

        })
    }

    override fun createModel(): TransferAccountsContract.Model = TransferAccountsModel()

    override fun loadData() {
        getModel().loadData(object : RxHttpObserver<TransferAccountsEntity>() {
            override fun onCompleted(msg: String?, entity: TransferAccountsEntity?) {
                getView()?.setData(entity)
            }

            override fun onError(error: String?) {
                getView()?.showToast(error)
            }

            override fun onLoadingStart() {
                attachObserver(this)
                getView()?.showLoadingDialog()
            }

            override fun onLoadingFinish() {
                getView()?.dismissLoadingDialog()
            }
        })
    }

    override fun transfer() {
        val payeeUserMobile = getView()!!.payeeUserMobile
        val walletMoney = getView()!!.walletMoney
        val integralNumber = getView()!!.integralNumber
        val remark = getView()!!.remark
        val payPwd = getView()!!.payPwd

        if (checkTransfer()) {
            getModel().transfer(payeeUserMobile, walletMoney, integralNumber, remark,payPwd, object : RxHttpObserver<Any>() {
                override fun onCompleted(msg: String?, entity: Any?) {
                    getView()!!.showToast(msg)
                    getView()!!.startActivityThenFinishSelf(TransferAccountsSuccessActivity::class.java)
                }

                override fun onError(error: String?) {
                    getView()!!.showToast(error)
                }

                override fun onLoadingStart() {
                    attachObserver(this)
                    getView()!!.showLoadingDialog("提交申请中")
                }

                override fun onLoadingFinish() {
                    getView()!!.dismissLoadingDialog()
                }
            })
        }

    }

    override fun checkPayPwd() {
        getModel().checkPayPwd(object : RxHttpObserver<CheckPwdEntity>(){

            override fun onCompleted(msg: String?, entity: CheckPwdEntity?) {
                if ("0"==entity!!.pwd_status){
                    getView()?.onSettingPayPwd()
                }else{
                    getView()?.showPayPwdDialog()
                }
            }

            override fun onError(error: String?) {
                getView()!!.showToast(error)
            }

            override fun onLoadingStart() {
                attachObserver(this)
                getView()?.showLoadingDialog()
            }

            override fun onLoadingFinish() {
                getView()?.dismissLoadingDialog()
            }

        })
    }

    /**
     * 检查转账参数
     */
    private fun checkTransfer(): Boolean {

        val payeeUserMobile = getView()!!.payeeUserMobile
        val walletMoney = getView()!!.walletMoney
        val integralNumber = getView()!!.integralNumber




        if (TextUtils.isEmpty(payeeUserMobile) || payeeUserMobile.length != 11) {
            getView()?.showToast(application.getString(R.string.Please_enter_the_correct_account_number))
            return false
        }

        //零钱,积分至少传一个
        if (TextUtils.isEmpty(walletMoney) && TextUtils.isEmpty(integralNumber)) {
            //两个都为空
            getView()?.showToast(application.getString(R.string.Please_enter_the_amount_of_transfer))
            return false
        }
        if (!TextUtils.isEmpty(walletMoney) && walletMoney.toDouble() == 0.00) {
            getView()?.showToast(application.getString(R.string.Please_enter_your_change))
            return false
        }
        if (!TextUtils.isEmpty(integralNumber) && integralNumber.toDouble() == 0.00) {
                getView()?.showToast(application.getString(R.string.Please_enter_a_valid_gift_certificate))
            return false
        }
        return true
    }
}
